<?php

namespace App\Http\Controllers\Home;


use App\Model\people;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class VideoController extends Controller
{
    public function index(){
        return view('video');
    }

    public function handle(Request $request){
        $info = $request -> post();

        // 1.登录获取token
        $login_url = "https://hgzjpx.e.hbzclm.com/prod-api/auth/login";
        $login_post_data = [
            "username" => $info['username'],
            "password" => $info['password'],
            "entpId" => $info['entpId'],
        ];


        $login_res = curlPost($login_url, $login_post_data, 1);

        $res_data = json_decode($login_res);

        if($res_data->code == 500){
            $abc = "身份证号有问题，无法登录";
            return view('video',compact('abc'));
        }

        //2.获取packagebagId和regId
        $getId_url = "https://hgzjpx.e.hbzclm.com/prod-api/eceping-course/home/getCoursebypackagebagFromHome?packagebagStatus=0&entpId=77";

        $header = array(
            'Content-Type: application/json',
            'Authorization: Bearer '.$res_data->data->access_token
        );

        $getId_res = json_decode(curlPost($getId_url,'',0,$header));

        $packagebagId = $getId_res->data[0]->packagebagId;
        $regId = $getId_res->data[0]->regId;

        //3.获取用户课程学习详情
        $course_url = 'https://hgzjpx.e.hbzclm.com/prod-api/eceping-course/home/getUserCourseStudyDetail?courseId=266&regId='.$regId.'&studyEntpId=&entpId=77';

        $course_res = json_decode(curlPost($course_url,'',0,$header),true);

        $courseCatalogList = $course_res['data']['courseCatalogList'];

        $courseList = array();

        foreach ($courseCatalogList as $cousecate) {
            foreach ($cousecate['cousecate'] as $course_detail) {
                $timeString = $course_detail['resouceHours'];
                list($h, $m, $s) = explode(':', $timeString);
                $time = $h * 3600 + $m * 60 + $s;
//                dd($time);
                $info_arr = [
                    'ccId'=>$course_detail['ccId'],
                    'time'=>$time - 5
                ];

                array_push($courseList, $info_arr);
            }
        }
        $saveUserTime_url = 'https://hgzjpx.e.hbzclm.com/prod-api/eceping-course/home/saveUserStudyTime';

        $saveUser_data = [
            "packagebagId"=>$packagebagId,
            "regId"=>$regId,
            "courseId"=>266,
            "ccId"=>3168,
            "courseDelayTime"=>3200,
            "courseOver"=>1,
            "studyEntpId"=>'',
            "entpId"=>77
        ];

        foreach ($courseList as $value) {
            $saveUser_data['ccId'] = $value['ccId'];
            $saveUser_data['courseDelayTime'] = $value['time'];
            curlPost($saveUserTime_url,$saveUser_data,1,$header);
            sleep(1);
        }



        if($info['username']){
            $abc = $info['username']."处理成功";

            $a['num'] = $info['username'];

            people::create($a);
        }else{
            $abc = '';
        }
        return view('video',compact('abc'));
    }
}
